using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Wikey.Domain.Repository;
using Wikey.Extensibility.Interfaces;

namespace Wikey.Domain.Components {
    public interface IDynamicDataSource {
        JsonResult Get(string source, string type);
    }


    public class DynamicDataSource : IDynamicDataSource {
        private Dictionary<string, IDynamicGet> Datasource;

        public DynamicDataSource() {
            Initialize();
        }

        public JsonResult Get(string source, string type) {
            return Datasource[source].Get();
        }

        private void Initialize() {
            Datasource =
                new Dictionary<string, IDynamicGet>
                    {
                        {"Spaces", new GetSpaces()},
                        {"Pages", new GetPages()},
                        {"Users" , new GetUsers()},
                        {"Templates" , new GetTemplates()},
                        {"Comments" , new GetComments()} ,
                        {"ContentNodes" , new GetContentNodes()} , 
                        {"Macros" , new GetMacros()},
                        {"Tags" , new GetTags()} , 
                        {"Documents" , new GetDocuments()}
                    };
        }
    }

    internal class GetDocuments : IDynamicGet
    {
        public JsonResult Get()
        {
            throw new NotImplementedException();
        }
    }

    internal class GetTags : IDynamicGet
    {
        public JsonResult Get()
        {
            throw new NotImplementedException();
        }
    }

    internal class GetMacros : IDynamicGet
    {
        public JsonResult Get()
        {
            throw new NotImplementedException();
        }
    }

    internal class GetContentNodes : IDynamicGet
    {
        public JsonResult Get()
        {
            throw new NotImplementedException();
        }
    }

    internal class GetComments : IDynamicGet
    {
        public JsonResult Get()
        {
            throw new NotImplementedException();
        }
    }

    internal class GetTemplates : IDynamicGet
    {
        public JsonResult Get()
        {
            throw new NotImplementedException();
        }
    }

    internal class GetUsers : IDynamicGet
    {
        public JsonResult Get()
        {
            var listOfUsers = Factory.Get<IUserRepository>().SelectAll();
            var resultJson = new List<BaseJsonObject>();
            foreach (var user in listOfUsers) {
                resultJson.Add(new BaseJsonObject { Id = user.Id, Name = user.Name , FriendlyUrl = user.Name});
            }

            var result =
                new JsonResult
                {
                    Data = resultJson,
                    ContentType = typeof(BaseJsonObject).FullName,
                   
                };
            return result;
        }
    }

    public class BaseJsonObject {
        public int Id { get; set; }
        public string Name { get; set; }
        public string FriendlyUrl { get; set; }
    }
    internal class GetPages : IDynamicGet {

        public JsonResult Get() {
            var repo = Factory.Get<IPageRepository>();
            var listOfPages = repo.Selectable().Take(10).OrderBy(t=>t.Name);
            var resultJson = new List<BaseJsonObject>();
            foreach (var page in listOfPages) {
                resultJson.Add(new BaseJsonObject { Id = page.Id, Name = page.Name , FriendlyUrl = page.FriendlyName});
            }

            var result =
                new JsonResult
                    {
                        Data = resultJson,
                        ContentType = typeof(BaseJsonObject).FullName,
                        JsonRequestBehavior = JsonRequestBehavior.AllowGet
                    };
            return result;
        }
    }

    public interface IDynamicGet {
        JsonResult Get();
    }

    internal class GetSpaces : IDynamicGet {
        public JsonResult Get() {
            var list = Factory.Get<ISpaceRepository>().SelectAll();
            var resultJson = new List<BaseJsonObject>();
            foreach (var space in list) {
                resultJson.Add(new BaseJsonObject { Id = space.Id, Name = space.Name , FriendlyUrl = space.FriendlyUrlName});
            }

            var result =
                new JsonResult
                {
                    Data = resultJson,JsonRequestBehavior = JsonRequestBehavior.AllowGet,
                    ContentType = typeof(BaseJsonObject).FullName
                };
            return result;
        }
    }
}